In JavaScript, cookies are small pieces of data stored on the user's browser. They are primarily used for storing information that needs to persist across sessions or page reloads, such as:
You can create a cookie using the document.cookie property:
username=JohnDoe → Sets the key-value pair for the cookie.expires=Fri, 31 Dec 2025 23:59:59 GMT → Sets the expiration date.path=/ → Makes the cookie accessible across the entire site.You can read a cookie like this:
Output:
To delete a cookie, set its expires date to a past date: